<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CategoryForm
 *
 * @author WIN7
 */
class CategoryForm extends CFormModel
{
        public $namecategory;
        public $image;
        /**
	 * Declares the validation rules.
	 * 
	 * 
	 */
	public function rules()
	{
		return array(
			
			array('namecategory', 'required'),
                                                         array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
                                                         array('title, image', 'length', 'max'=>255, 'on'=>'insert,update'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
        
        public function attributeLabels()
	{		
	}
                    
	/**
	 */
	public function add(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection  
//                                  $link_image= $_POST['CategoryForm']['image'];
                                 //
//                                 $link_image =  $this->LinkImage;
//                                 $part = explode ('/', $link_image);
//                                 $image = $part[count($part)-1];
                                 $query="INSERT INTO os_category(NameCategory,LinkImage,sale_id) VALUE('{$this->namecategory}','{$this->image}',".Yii::app()->session['saleId'].")";
                                 $connection->createCommand($query)->query();   
                                 return true;
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
                        public function edit(){              
                        try{
                                 $connection=Yii::app()->db;   // assuming you have configured a "db" connection  
                                 //find Id from rowId in jqGrid
                                 $query_id="select os_category.Id, os_category.LinkImage from os_category 
                                     where os_category.sale_id =".Yii::app()->session["saleId"];
                                 $command_id = $connection->createCommand($query_id);
                                 $dataReader_id=$command_id->query();   
                                 $rows_category = array();
                                while(($row=$dataReader_id->read())!==false) {
                                    $rows_category[]=$row;
                                }
                                //Id in os_category table;
                                $rowId = $_GET['id']-1;
                                $categoryId = $rows_category[$rowId]['Id'];
                                //update to database
                                if (!empty($rows_category[$rowId]['LinkImage']) && empty($this->image)) {
                                $this->image = $rows_category[$rowId]['LinkImage'];
                                }
                                $query="UPDATE os_category as a SET a.NameCategory='{$this->namecategory}', a.LinkImage='{$this->image}' WHERE a.Id=".$categoryId; 
                                $connection->createCommand($query)->execute();
//                                $command->bindParam(":name",  $this->name,PDO::PARAM_STR);
//                                $command->bindParam(":mobile",  $this->mobile,PDO::PARAM_STR);
//                                $command->bindParam(":address",  $this->address,PDO::PARAM_STR);
//                                $command->bindParam(":lat",  $this->lat,PDO::PARAM_STR);
//                                $command->bindParam(":lng",  $this->lng,PDO::PARAM_STR);
//                                $command->bindParam(":image",  $this->image,PDO::PARAM_STR);
//                                $command->bindParam(":id",$store_id,PDO::PARAM_INT);
//                                $row_count=$command->execute();
//                                $resul=array('data'=>$json,'status'=>'ok');
//                                $string=json_encode($resul);
//                                echo $string;                                                              
                                 return true;
                                
                       }catch(Exception $e){
                                 $result=array('data'=>'','status'=>'fail','error'=>$e->getMessage());
                                 echo $string=json_encode($result);
                                 return FALSE;
                                 }                    
                        }
                        public function getCategory($id) {
                            $connection = Yii::app()->db;
                            $sql = "select * from os_category where sale_id =".$id;
                            $data = $connection->createCommand($sql)->query();
                            $rows_category = array();
                            while ($row = $data->read()) {
                                $rows_category[] = $row;
                                }
                                return $rows_category;
                        }
}


?>